<template>
    <div class="student-index" style="background:#fff; overflow:hidden;">
        <div class="tab-nav" v-if="navData.length > 2">
            <span>专业:</span>
            <ul>
                <li v-for="(item, index) in navData" :class="{ 'active': activeIndex == index }" @click="tabNav(item, index)">{{ item.name }}</li>
            </ul>
        </div>
        <!-- <el-row class="student-index-title">
            <el-col :span="24">实训案例</el-col>
            <el-col :span="24" style="margin-top: 12px;">Training project</el-col>
        </el-row> -->
        <div class="search-box">
            <div class="search-item">
                <span class="search-title">行业类别: </span>
                <el-select
                    v-model="industryValue"
                    placeholder="请选择行业"
                    value-key="value"
                    style="float:left;">
                    <el-option
                    v-for="item in industryStatus"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-item">
                <span class="search-title">案例名称: </span>
                <el-input v-model="caseName" placeholder="请输入关键字" style="float:left; width:auto;"></el-input>
            </div>
            <div class="search-item">
                <el-button type="primary" @click="searchFn">搜索</el-button>
            </div>
        </div>
        <div class="student-index-list" v-if="listData.length > 0">
            <el-row :gutter="30">
                <el-col :span="6" v-for="(item, index) in listData" :key="index">
                    <div class="list-item" @click="storageProjectId(item.id, '/teacher/projectintroduction',item.showEndItemFlag, item.presetChapterId, item.name)">
                        <div class="list-header" :style="{ background: item.colour }">
                            <div class="list-img">
                                <img :src="item.picUrl" />
                            </div>
                            <div class="list-text1">{{ item.name }}</div>
                        </div>
                        <div class="list-content">
                            <p>{{ item.description }}</p>
                        </div>
                        <div class="list-content-knot" v-if="item.showEndItemFlag" @click="storageProjectId(item.id, '/teacher/projectintroduction',item.showEndItemFlag, item.name)">
                            <img src="./../../assets/images/knot_icon.png" />
                        </div>
                        <div class="list-tag" :style="{ background: item.colour }" :title="item.typeName">{{ item.typeName }}</div>
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="student-tips" v-else style="text-align:center; padding:5% 0 10%;">
            <img src="./../../assets/images/project_tips3.jpg" />
        </div>
    </div>
</template>

<script>
    import { mapState } from 'vuex'
    export default {
        data () {
            return {
                listData:[],
                industryStatus: [
                    {
                        value: '',
                        label: '全部'
                    }
                ],
                industryValue: '',
                caseName: '',

                navData: [
                    {
                        id: '',
                        name: '全部'
                    }
                ],
                activeIndex: '0',
                activeId: ''
            }
        },
        created () {
            this.initSelect()
            this.init()     //初始化
            this.listInit()

           // localStorage.removeItem('teacher-project-id')
            // localStorage.removeItem('teacher-project-ida')
        },
        computed:{
			...mapState(['itemServiceValue', 'chapterType','itemServiceType', 'projectBreadCrumbBack'])
		},
        methods: {
            initSelect(){
                let self = this
                // self.$http.get('/dataplatform/major/list').then(response => {
                //     let res = response.data
                //     if(res.code == '200'){
                //         for (let i = 0; i < res.data.length; i++) {
                //             let json = {}
                //             json.id = res.data[i].id
                //             json.name = res.data[i].name
                //             self.navData.push(json)
                //         }
                //     }else if(res.code == '401'){
                //         self.$router.replace({
                //             path:'/login'
                //         })
                //     }else{
                //         self.$message.error(res.message)
                //     }
                // })
                self.$http.get('/dataplatform/item/industryType').then(response => {
                    let res = response.data
                    if(res.code == '200'){
                        for (let i = 0; i < res.data.length; i++) {
                            let json = {}
                            json.id = res.data[i].id
                            json.name = res.data[i].name
                            self.navData.push(json)
                        }
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            // 切换nav
            tabNav(item, index){
                let self = this
                self.activeIndex = index
                self.activeId = item.id
                self.listInit()

            },
            init() {
                let self = this
                self.$http.get('/dataplatform/common/systemConfig/list?searchConfigs=industryType').then(response => {
                    let res = response.data
                    if(res.code == '200'){
                        for (let i = 0; i < res.data.length; i++) {
                            let industryJson = {}
                            industryJson.value = res.data[i].id
                            industryJson.label = res.data[i].keyValue
                            self.industryStatus.push(industryJson)
                        }
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            listInit(){
                let self = this
                self.$http.get('/dataplatform/item/list?itemServiceType=1&systemConfigId='+self.industryValue+'&name='+self.caseName+'&majorId='+self.activeId).then((response) => {
                    let res = response.data
                    if(res.code == '200'){
                        self.listData = res.data
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            //存储案例ID
            storageProjectId(id,path,showEndItemFlag, chapterId, name){
                let self = this
                if(showEndItemFlag){
                    self.$message({
                        message: '已结项',
                        type: 'warning'
                    })
                    return
                }
                let result = {
                    itemId: id,
                    type: 1
                }
                localStorage.setItem('teacher-project-ida', JSON.stringify(result))
                

                self.$http.get('/dataplatform/item/menu?itemId='+id+'&itemServiceType=1').then(response => {
                    let res = response.data
                    if(res.code == '200'){
                        this.$store.commit('updateItemServiceValue', res.data)
                        this.$store.commit('updateChapterType', chapterId)
                        this.$store.commit('updateItemServiceType', 1)
                        this.$store.commit('updateProjectBreadCrumbBack', name)
                        self.$router.push({
                            path: path
                        })
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            //跳转step获取章节ID
            goStep(id, path, type,showEndItemFlag){
                let self = this
                if(showEndItemFlag){
                    self.$message({
                        message: '已结项',
                        type: 'warning'
                    })
                    return
                }
                //type==1预置2自定义
                let result = {
                    itemId: id,
                    type: type
                }
                localStorage.setItem('teacher-project-id', JSON.stringify(result))
                self.$http.get('/dataplatform/chapter/getChapterId?itemId='+id+'&chapterType='+type).then(response => {
                    let res = response.data
                    if(res.code == '200'){
                        this.$store.commit('updateChapterType', res.data)
                        this.$store.commit('updateItemServiceType', 1)
                        self.$router.push({
                            path: path
                        })
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            searchFn(){
                this.listInit()
            }
        }
    }
</script>

<style lang="scss">
    @import '@/assets/styles/student.scss';
    .tab-nav{
        margin: 50px 0 0 60px;
        padding-bottom: 20px;
        border-bottom: solid 1px #dcdfe6;
        overflow: hidden;
        span{
            float: left;
            font-size: 14px;
            font-weight: bold;
            color: #333333;
            height: 36px;
            line-height: 36px;
        }
        ul{
            margin-left: 20px;
            li{
                height: 36px;
                line-height: 36px;
                float: left;
                font-size: 14px;
                padding:0 8px;
                box-sizing: border-box;
                cursor: pointer;
                margin: 0 15px;
                &.active{
                    background: #fff0f0;
                    color: #e31d1a;
                    border-radius: 5px;
                }
            }
        }
    }
</style>
